
package com.contactmodule.dao;
import com.contactmodule.bean.TbDepartPost;
import java.sql.*;
import java.io.*;
import java.util.*;
import util.*;
public class TbDepartPostDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbDepartPost tbDepartPost=null;
    private List<TbDepartPost> tbDepartPostList=null;

    public boolean add(TbDepartPost inTbDepartPost){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_DepartPost(DepartId,PostId,CreateTime) values(?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbDepartPost.getDepartId());
        pt.setInt(2,inTbDepartPost.getPostId());
        pt.setString(3,inTbDepartPost.getCreateTime());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inDepartPostId){
        return UtilDAO.del("Tb_DepartPost", "DepartPostId", inDepartPostId);
    }
    public boolean edit(TbDepartPost inTbDepartPost){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_DepartPost set DepartPostId=?,DepartId=?,PostId=?,CreateTime=? where DepartPostId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbDepartPost.getDepartPostId());
        pt.setInt(2,inTbDepartPost.getDepartId());
        pt.setInt(3,inTbDepartPost.getPostId());
        pt.setString(4,inTbDepartPost.getCreateTime());
        pt.setInt(5,inTbDepartPost.getDepartPostId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbDepartPost getTbDepartPostByDepartPostId(int inDepartPostId) {
        tbDepartPost=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DepartPost where DepartPostId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inDepartPostId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbDepartPost == null)tbDepartPost = getTbDepartPost(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDepartPost;
    }
    public List<TbDepartPost> getTbDepartPostAll() {
        tbDepartPostList=new ArrayList<TbDepartPost>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DepartPost";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbDepartPostList.add(getTbDepartPost(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDepartPostList;
    }
    public List<TbDepartPost> getTbDepartPostAll(VskPage vp) {
        tbDepartPostList=new ArrayList<TbDepartPost>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_DepartPost";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_DepartPost", "DepartPostId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbDepartPostList.add(getTbDepartPost(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbDepartPostList;
    }
    public TbDepartPost getTbDepartPost(ResultSet rs) throws Exception{
        TbDepartPost tbDepartPost = new TbDepartPost();
        tbDepartPost.setDepartPostId(rs.getInt("DepartPostId"));
        tbDepartPost.setDepartId(rs.getInt("DepartId"));
        tbDepartPost.setPostId(rs.getInt("PostId"));
        tbDepartPost.setCreateTime(rs.getString("CreateTime"));
        return tbDepartPost;
    }
}
